﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows;

namespace Company.UI.CustomControls
{
    public class CustomButton : Button
    {
        static CustomButton()
        {
            FrameworkElement.DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomButton), new FrameworkPropertyMetadata(typeof(CustomButton)));
        }

        protected override void OnClick()
        {
            base.OnClick();
            if (this.SimpleCommand != null)
            {
                this.SimpleCommand.Execute(base.CommandParameter);
            }
        }
        public ICommand SimpleCommand
        {
            get
            {
                return (ICommand)base.GetValue(CustomButton.SimpleCommandProperty);
            }
            set
            {
                base.SetValue(CustomButton.SimpleCommandProperty, value);
            }
        }

        private static void SimpleCommandPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)
        {
            CustomButton CustomButton = dependencyObject as CustomButton;
            if (CustomButton != null)
            {
                CustomButton.OnSimpleCommandChanged((ICommand)e.OldValue, (ICommand)e.NewValue);
            }
        }

        private void OnSimpleCommandChanged(ICommand oldValue, ICommand newValue)
        {
            if (oldValue != null)
            {
                this.UnhookCommand(oldValue);
            }
            this.HookupCommand(newValue);
            this.CanExecuteChanged(null, null);
        }

        private void UnhookCommand(ICommand oldCommand)
        {
            oldCommand.CanExecuteChanged -= this.CanExecuteChanged;
        }

        private void HookupCommand(ICommand newCommand)
        {
            EventHandler value = new EventHandler(this.CanExecuteChanged);
            if (newCommand != null)
            {
                newCommand.CanExecuteChanged += value;
            }
        }

        private void CanExecuteChanged(object sender, EventArgs e)
        {
            if (this.SimpleCommand != null)
            {
                RoutedCommand routedCommand = this.SimpleCommand as RoutedCommand;
                if (routedCommand != null)
                {
                    base.IsEnabled = routedCommand.CanExecute(base.CommandParameter, base.CommandTarget);
                    return;
                }
                base.IsEnabled = this.SimpleCommand.CanExecute(base.CommandParameter);
            }
        }

        public SolidColorBrush ButtonMouseOver
        {
            get
            {
                return new SolidColorBrush();
            }
            set
            {
                base.SetValue(CustomButton.ButtonPressedProperty, value);
            }
        }

        public SolidColorBrush ButtonPressed
        {
            get
            {
                return new SolidColorBrush();
            }
            set
            {
                base.SetValue(CustomButton.ButtonPressedProperty, value);
            }
        }

        public SolidColorBrush ButtonDisabled
        {
            get
            {
                return new SolidColorBrush();
            }
            set
            {
                base.SetValue(CustomButton.ButtonDisabledProperty, value);
            }
        }

        public SolidColorBrush ButtonPressedBorder
        {
            get
            {
                return new SolidColorBrush();
            }
            set
            {
                base.SetValue(CustomButton.ButtonPressedBorderProperty, value);
            }
        }

        public SolidColorBrush ButtonDisabledBorder
        {
            get
            {
                return new SolidColorBrush();
            }
            set
            {
                base.SetValue(CustomButton.ButtonDisabledProperty, value);
            }
        }
        public double CornerRadius
        {
            get
            {
                return (double)base.GetValue(CustomButton.CornerRadiusProperty);
            }
            set
            {
                base.SetValue(CustomButton.CornerRadiusProperty, value);
            }
        }

        public SolidColorBrush StatusActiveBrush
        {
            get
            {
                return (SolidColorBrush)base.GetValue(CustomButton.StatusActiveBrushProperty);
            }
            set
            {
                base.SetValue(CustomButton.StatusActiveBrushProperty, value);
            }
        }

        public SolidColorBrush StatusInactiveBrush
        {
            get
            {
                return (SolidColorBrush)base.GetValue(CustomButton.StatusInactiveBrushProperty);
            }
            set
            {
                base.SetValue(CustomButton.StatusInactiveBrushProperty, value);
            }
        }

        public bool? IsStatusActive
        {
            get
            {
                return (bool?)base.GetValue(CustomButton.IsStatusActiveProperty);
            }
            set
            {
                base.SetValue(CustomButton.IsStatusActiveProperty, value);
            }
        }

        public bool IsStatusBlinking
        {
            get
            {
                return (bool)base.GetValue(CustomButton.IsStatusBlinkingProperty);
            }
            set
            {
                base.SetValue(CustomButton.IsStatusBlinkingProperty, value);
            }
        }

        public GeometryGroup Icon
        {
            get
            {
                return (GeometryGroup)base.GetValue(CustomButton.IconProperty);
            }
            set
            {
                base.SetValue(CustomButton.IconProperty, value);
            }
        }

        public SolidColorBrush IconFillColor
        {
            get
            {
                return new SolidColorBrush(Color.FromRgb(0, byte.MaxValue, byte.MaxValue));
            }
            set
            {
                base.SetValue(CustomButton.IconFillColorProperty, value);
            }
        }

        public SolidColorBrush IconBorderColor
        {
            get
            {
                return new SolidColorBrush(Color.FromRgb(0, byte.MaxValue, byte.MaxValue));
            }
            set
            {
                base.SetValue(CustomButton.IconBorderColorProperty, value);
            }
        }

        public double IconHeight
        {
            get
            {
                return (double)base.GetValue(CustomButton.IconHeightProperty);
            }
            set
            {
                base.SetValue(CustomButton.IconHeightProperty, value);
            }
        }

        public double IconWidth
        {
            get
            {
                return (double)base.GetValue(CustomButton.IconWidthProperty);
            }
            set
            {
                base.SetValue(CustomButton.IconWidthProperty, value);
            }
        }

        public double IconStrokeThickness
        {
            get
            {
                return (double)base.GetValue(CustomButton.IconStrokeThicknessProperty);
            }
            set
            {
                base.SetValue(CustomButton.IconStrokeThicknessProperty, value);
            }
        }

        public bool IsToggled
        {
            get
            {
                return (bool)base.GetValue(CustomButton.IsToggledProperty);
            }
            set
            {
                base.SetValue(CustomButton.IsToggledProperty, value);
            }
        }

        public bool SupportIsExecuting
        {
            get
            {
                return (bool)base.GetValue(CustomButton.SupportIsExecutingProperty);
            }
            set
            {
                base.SetValue(CustomButton.SupportIsExecutingProperty, value);
            }
        }

        public static readonly DependencyProperty ButtonMouseOverProperty = DependencyProperty.Register("ButtonMouseOver", typeof(SolidColorBrush), typeof(CustomButton));

        public static readonly DependencyProperty ButtonPressedProperty = DependencyProperty.Register("ButtonPressed", typeof(SolidColorBrush), typeof(CustomButton));

        public static readonly DependencyProperty ButtonDisabledProperty = DependencyProperty.Register("ButtonDisabled", typeof(SolidColorBrush), typeof(CustomButton));

        public static readonly DependencyProperty ButtonPressedBorderProperty = DependencyProperty.Register("ButtonPressedBorder", typeof(SolidColorBrush), typeof(CustomButton));

        public static readonly DependencyProperty ButtonDisabledBorderProperty = DependencyProperty.Register("ButtonDisabledBorder", typeof(SolidColorBrush), typeof(CustomButton));

        public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register("CornerRadius", typeof(double), typeof(CustomButton));

        public static readonly DependencyProperty StatusActiveBrushProperty = DependencyProperty.Register("StatusActiveBrush", typeof(SolidColorBrush), typeof(CustomButton), new PropertyMetadata(Application.Current.TryFindResource("Brush.Good") ?? new BrushConverter().ConvertFromString("#00F976")));

        public static readonly DependencyProperty StatusInactiveBrushProperty = DependencyProperty.Register("StatusInactiveBrush", typeof(SolidColorBrush), typeof(CustomButton), new PropertyMetadata(Application.Current.TryFindResource("Brush.Overlay12Percent") ?? new BrushConverter().ConvertFromString("#404040")));

        public static readonly DependencyProperty IsStatusActiveProperty = DependencyProperty.Register("IsStatusActive", typeof(bool?), typeof(CustomButton));

        public static readonly DependencyProperty IsStatusBlinkingProperty = DependencyProperty.Register("IsStatusBlinking", typeof(bool), typeof(CustomButton));

        public static readonly DependencyProperty IconProperty = DependencyProperty.Register("Icon", typeof(GeometryGroup), typeof(CustomButton));

        public static readonly DependencyProperty IconFillColorProperty = DependencyProperty.Register("IconFillColor", typeof(SolidColorBrush), typeof(CustomButton), new PropertyMetadata(new SolidColorBrush(Color.FromArgb(0, 0, 0, 0))));

        public static readonly DependencyProperty IconBorderColorProperty = DependencyProperty.Register("IconBorderColor", typeof(SolidColorBrush), typeof(CustomButton), new PropertyMetadata(Application.Current.TryFindResource("Brush.Icon") ?? new SolidColorBrush(Color.FromRgb(byte.MaxValue, byte.MaxValue, byte.MaxValue))));

        public static readonly DependencyProperty IconHeightProperty = DependencyProperty.Register("IconHeight", typeof(double), typeof(CustomButton), new PropertyMetadata(20.0));

        public static readonly DependencyProperty IconWidthProperty = DependencyProperty.Register("IconWidth", typeof(double), typeof(CustomButton), new PropertyMetadata(20.0));

        public static readonly DependencyProperty IconStrokeThicknessProperty = DependencyProperty.Register("IconStrokeThickness", typeof(double), typeof(CustomButton), new PropertyMetadata(1.0));

        public static readonly DependencyProperty IsToggledProperty = DependencyProperty.Register("IsToggled", typeof(bool), typeof(CustomButton));

        public static readonly DependencyProperty SupportIsExecutingProperty = DependencyProperty.Register("SupportIsExecuting", typeof(bool), typeof(CustomButton), new PropertyMetadata(false));

        public static readonly DependencyProperty SimpleCommandProperty = DependencyProperty.Register("SimpleCommand", typeof(ICommand), typeof(CustomButton), new FrameworkPropertyMetadata(null, new PropertyChangedCallback(CustomButton.SimpleCommandPropertyChangedCallback)));
    }
}
